package com.zego.wawaji.ui.dialog.pay;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.ui.base.BaseDialogFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 充值方式底部弹出框
 * Created by Su on 2017/12/26.
 */
@SuppressLint("ValidFragment")
public class PayBottomDialog extends BaseDialogFragment {
    private Context mContext;
    private View dialogView;
    private Dialog dialog;
    private ListView lv_defriend;
    private View de_cancel;
    private OnDialogItemClick onDialogItemClick;

    private List<String> list = new ArrayList<>();
    private Map<String, Integer> itemInfos = new HashMap<>();
    private ArrayAdapter<String> mAdapter;

    private TextView popup_dialog_title;

    public PayBottomDialog() {
        super();
    }

    public PayBottomDialog(Context mContext, OnDialogItemClick onDialogItemClick) {
        this.onDialogItemClick = onDialogItemClick;
        this.mContext = mContext;
        initDialogView();
        initBottomDialog();
        getPayItem();
        initData();
    }

    private void initDialogView() {
        dialogView = View.inflate(mContext, R.layout.pay_bottom_dialog, null);
        lv_defriend = (ListView) dialogView.findViewById(R.id.lv_defriend);
        de_cancel = dialogView.findViewById(R.id.de_cancel);
        popup_dialog_title = (TextView) dialogView.findViewById(R.id.popup_dialog_title);
        //点击事件
        lv_defriend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onDialogItemClick != null)
                    onDialogItemClick.onDialogItemCilck(parent, view, position, id, itemInfos);
            }
        });
        de_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog();
                //TipToolUtils.dismiss();
            }
        });
    }

    private void initBottomDialog() {
        dialog = new Dialog(mContext);
        dialog.setContentView(dialogView);
        dialog.setCanceledOnTouchOutside(true);
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        WindowManager.LayoutParams params = window.getAttributes();
        params.dimAmount = 0.1f;
        params.width = (int) (ModuleMgr.getAppMgr().getScreenWidth() * 0.95);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.AnimDownInDownOut);
    }

    public void showDialog() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    /**
     * 支付弹框弹窗条目内容
     */
    private void getPayItem() {
        list.clear();
        list.add("微信支付");
        list.add("支付宝支付");
    }

    private void initData() {
        mAdapter = new ArrayAdapter<>(mContext, R.layout.pay_dialog_item, R.id.de_type, list);
        lv_defriend.setAdapter(mAdapter);
    }

    public interface OnDialogItemClick {
        /**
         * @param itemInfos key:条目文字内容  value == position + 1
         */
        void onDialogItemCilck(AdapterView<?> parent, View view, int position, long id, Map<String, Integer> itemInfos);
    }
}
